package com.han.bom;

/**
 * The Answer class represents an answer of a Student for a specified Question. 
 * @author Ric Janus Sapasap
 * @since JDK 1.7
 * @version 1.0
 * @see Student
 * @see Question
 *
 */
public class Answer {
	private Question question;
	private String answer;
	
	/**
	 * Sole Constructor, initializes an answer instance
	 * 
	 * @param question An instance of the Question Class
	 * @param answer A String representation of the answer as submitted by the student
	 */
	public Answer(Question question, String answer) {
		super();
		this.question = question;
		this.answer = answer;
	}

	/**
	 * 
	 * @return the Question instance of the object
	 */
	public Question getQuestion() {
		return question;
	}

	/**
	 * Sets this Answer's Question attribute to the specified question
	 * @param question a Question instance
	 */
	public void setQuestion(Question question) {
		this.question = question;
	}

	/**
	 * Gets the string submitted by the student 
	 * @return string answer
	 */
	public String getAnswer() {
		return answer;
	}

	/**
	 * sets this instance's answer
	 * @param answer the string answer
	 */
	public void setAnswer(String answer) {
		this.answer = answer;
	}
	
	/* (non-Javadoc)
	 * returns a String representation of this instance
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Answer [question=" + question + ", answer=" + answer + "]";
	}

	/* (non-Javadoc)
	 * returns the hash code for this Answer
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((answer == null) ? 0 : answer.hashCode());
		result = prime * result
				+ ((question == null) ? 0 : question.hashCode());
		return result;
	}

	/* (non-Javadoc)
	 * compares this answer to the specified parameter 
	 * @see java.lang.Object#equals(java.lang.Object)
	 * @return returns true if both the question and answer attributes are equal, false otherwise
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Answer other = (Answer) obj;
		if (answer == null) {
			if (other.answer != null)
				return false;
		} else if (!answer.equals(other.answer))
			return false;
		if (question == null) {
			if (other.question != null)
				return false;
		} else if (!question.equals(other.question))
			return false;
		return true;
	}

	/**
	 * determines whether the answer to the question is correct
	 * @return returns true if the answer submitted is the same as the answer to the question, false otherwise
	 */
	public boolean isCorrect(){
		boolean correct = false;
		
		if(answer.equals(question.getAnswer())){
			correct = true;
		}
		
		return correct;
	}
}
